/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-29
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-14
 * @Description: 文本输入框组件，支持右侧自定义插槽
 */
import 'package:flutter/material.dart';

class InputField extends StatefulWidget {
  // 前缀文字标签
  final IconData? icon;

  // 文本控制器
  final TextEditingController controller;

  // change 回调
  final void Function(String)? onChanged;

  // 文本
  final int maxLength;

  // 占位提示符
  final String? hintText;

  // lable
  final String? labelText;

  // 是否为密码输入框
  final bool isPassword;

  // 输入框类型
  final TextInputType? keyboardType;

  // 是否自动聚焦
  final bool autoFocus;

  // 后缀组件
  final Widget? rightSlot;

  // 是否为 简约风格
  final bool simplified;

  const InputField({
    super.key,
    this.maxLength = 20,
    required this.controller,
    this.icon = Icons.error,
    this.hintText,
    this.labelText,
    this.isPassword = false,
    this.keyboardType,
    this.autoFocus = false,
    this.rightSlot,
    this.simplified = false,
    this.onChanged,
  });

  @override
  InputFieldState createState() => InputFieldState();
}

class InputFieldState extends State<InputField> {
  late bool isObscure; // 维护密码可见性状态
  late FocusNode _focusNode;
  final Color _defaultColor = Color(0x99000000); // 默认
  final Color _focusClolor = Color(0xFF99A8F1); // 聚焦颜色
  Color _iconColor = Color(0x99000000); // 图标颜色

  @override
  void initState() {
    super.initState();
    isObscure = widget.isPassword;

    _focusNode = FocusNode();

    // 监听焦点变化
    _focusNode.addListener(() {
      setState(() {
        // 当获得焦点时，设置图标颜色为和光标相同
        _iconColor = _focusNode.hasFocus ? _focusClolor : _defaultColor;
      });
    });
  }

  @override
  void dispose() {
    _focusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Row(
      children: [
        Expanded(
            child: TextField(
          focusNode: _focusNode, // 绑定 FocusNode
          maxLength: widget.maxLength, // 最大输入长度
          controller: widget.controller,
          keyboardType: widget.keyboardType,
          obscureText: isObscure, // 控制密码可见性
          decoration: _buildInputDecoration(),
          onChanged: widget.onChanged,
        )),
      ],
    );
  }

  Widget? _buildPassword() {
    if (widget.isPassword) {
      return IconButton(
        icon: Icon(isObscure ? Icons.visibility_off : Icons.visibility,
            color: _iconColor),
        onPressed: () {
          setState(() {
            isObscure = !isObscure;
          });
        },
      );
    } else {
      return Icon(
        widget.icon,
        color: _iconColor,
      );
    }
  }

  _buildInputDecoration() {
    if (widget.simplified) {
      return InputDecoration(
        prefixIcon: _buildPassword(),
        labelText: widget.labelText,
        hintText: widget.hintText,
        fillColor: Colors.transparent, // 输入框背景色
        border: UnderlineInputBorder(
          borderSide: BorderSide(color: _defaultColor), // 下边框颜色
        ),
        enabledBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: _defaultColor), // 未聚焦时下边框颜色
        ),
        focusedBorder: UnderlineInputBorder(
          borderSide: BorderSide(color: _focusClolor), // 聚焦时下边框颜色
        ),
        contentPadding: EdgeInsets.symmetric(vertical: 22.0), // 增加上下内边距
        //  验证码图片，按钮等
        suffixIcon: widget.rightSlot,
      );
    } else {
      return InputDecoration(
        prefixIcon: _buildPassword(),
        labelText: widget.labelText,
        hintText: widget.hintText,
        suffixIcon: widget.rightSlot != null
            ? Padding(
                padding: EdgeInsets.only(right: 2), // 右侧增加2间距
                child: widget.rightSlot,
              )
            : null,
      );
    }
  }
}
